function extractTableHeaders() {
    const headers = [];
    const indices = {};

    const headerElements = document.querySelectorAll('div[role="columnheader"]');

    headerElements.forEach((header, index) => {
        const button = header.querySelector('button[title="Toggle SortBy"]');
        if (button) {
            const headerDiv = button.querySelector('div:first-child');
            if (headerDiv && headerDiv.textContent) {
                const columnName = headerDiv.textContent.trim();
                headers.push(columnName);
                indices[columnName] = index; // 记录字段对应的原始索引
            }
        }
    });

    return { headers, indices };
}

// 监听来自弹出页面的请求
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.action === 'getTableHeaders') {
        const { headers, indices } = extractTableHeaders();
        sendResponse({ headers, indices });
    }
    return true; // 保持消息通道开放
});